/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMI;

import java.sql.ResultSet;
import java.util.Collections;

/**
 *
 * @author TNT
 */
public class Product {
    
    Connect connect;
    ResultSet rs;
    String chuoi[];
    public Product(String chuoi[]){
        this.chuoi = chuoi;
        connect = new Connect(chuoi);
    }
    public String[] getProduct(){
        
        String sql = "Select * from product";
        java.util.List<String> Product= new java.util.ArrayList<String>();
        try{
            rs = connect.st.executeQuery(sql);
            while(rs.next()){
                String Info = rs.getString(1)+"###"+rs.getString(2)+"###"+rs.getString(3)+"###"+rs.getString(4)+"###"+rs.getString(5);
                Product.add(Info);
            }
            connect.con.close();
        }
        catch(Exception ex){}
        System.out.println(Product.size());
        Collections.shuffle(Product);
        String[] Prd = new String[9];
        for(int i = 0; i<9;i++){
            Prd[i] = Product.remove(0);
        }
        return Prd;
    }
    public String[] getAll(){
       String sql = "Select * from product";
        java.util.List<String> Pro= new java.util.ArrayList<String>();
        try{
            rs = connect.st.executeQuery(sql);
            while(rs.next()){
                String Info = rs.getString(1)+"###"+rs.getString(2)+"###"+rs.getString(3)+"###"+rs.getString(4)+"###"+rs.getString(5);
                Pro.add(Info);
            }
            connect.con.close();
        }
        catch(Exception ex){}
        
        Collections.shuffle(Pro);
        int count = Pro.size();
        String[] Prd = new String[20];
        for(int i = 0; i<count;i++){
            Prd[i] = Pro.remove(0);
        }
        return Prd;
    }
    public String[] getProductDetail(String ID){
        String[] Prd = new String[6];
        String sql = "select * from product where ID ='"+ID+"'";
        try{
            rs = connect.st.executeQuery(sql);
            rs.next();
                Prd[0] = rs.getString("Name");
                Prd[1] = rs.getString("Provider");
                Prd[2] = rs.getString("Cost");
                Prd[3] = rs.getString("Image");
                Prd[4] = rs.getString("Detail");
                Prd[5] = rs.getString("State");
                
            
        }
        catch(Exception ex){}
        return Prd;
    }
    public void editProduct(String id, String name, String provider, String cost, String detail, String state){
        String sql  = "UPDATE product SET name='"+name+"', provider='"+provider+"', price='"+cost+"', detail='"+detail+"', state='"+state+"' WHERE ID='"+id+"'";
        try{
            connect.st.execute(sql);
        }
        catch(Exception ex){}
    }
}
